﻿// -----------------------------------------------------------------------
// <copyright file="GeometricMeanFilter.cs" company="FCIS">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace LastVersionOfIpPackage.Operations
{
    using System;
    using System.Collections.Generic;
    using System.Drawing;
    using System.Linq;
    using System.Text;

    /// <summary>
    /// TODO: apply geometric mean filter to remove noise.
    /// </summary>
    public class GeometricMeanFilter
    {
        /// <summary>
        /// generate the Smothings mean mask based on the mask size.
        /// </summary>
        /// <param name="maskSize">Size of the mask.</param>
        /// <returns>the mask</returns>
        public Mask MeanFilter(int maskSize)
        {
            try
            {
                Mask m;
                m.MaskSize = maskSize;
                m.MaskValues = new double[maskSize, maskSize];
                double value = 1.0 / (maskSize * maskSize);
                for (int i = 0; i < maskSize; i++)
                {
                    for (int j = 0; j < maskSize; j++)
                    {
                        m.MaskValues[i, j] = value;
                    }
                }

                return m;
            }
            catch (Exception ex)
            {
                Logger.LogException(ex);
                throw ex;
            }
        }

        /// <summary>
        /// Applies the GM filter.
        /// </summary>
        /// <param name="image">The image.</param>
        /// <param name="maskSize">Size of the mask.</param>
        /// <param name="orignalImg">The orignal img.</param>
        /// <returns>
        /// the result of apply geometric mean
        /// </returns>
        public Bitmap ApplyGMFilter(Bitmap image, double maskSize, Color[,] orignalImg)
        {
            Color[,] buffer = LinearConvolution.LinerConvalution.AddBorder(orignalImg, maskSize);
             buffer = LinearConvolution.LinerConvalution.LinearConvolutionGeometricFn(image, buffer, maskSize);
             return BufferOfImage.Buffer.GetImage(buffer); 
        }
    }
}
